package system.config;

import framework.config.FileConfig;
import framework.config.S3Config;
import framework.exceptions.ConfigurationException;
import framework.storage.FileStorage;
import framework.storage.FileStorageLocal;
import framework.storage.FileStorageNone;
import framework.storage.FileStorageS3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 存储系统配置
 */
@Configuration
@Slf4j
public class FileStorageConfig {

    @Bean
    public FileStorage fileStorage(FileConfig fileConfig, S3Config s3Config) throws ConfigurationException {
        FileStorage fileStorage = null;

        //
        if ("local".equals(fileConfig.getStorageType())) {
            fileStorage = new FileStorageLocal(fileConfig);
        } else if ("s3".equals(fileConfig.getStorageType())) {
            fileStorage = new FileStorageS3(s3Config);
        } else if ("none".equals(fileConfig.getStorageType())) {
            fileStorage = new FileStorageNone();
        } else {
            throw new ConfigurationException("storage driver: not configured");
        }

        //
        log.info("Enable fileStorage: {}", fileStorage.getClass().getSimpleName());
        return fileStorage;
    }

}
